package com.topup.Controller;

import com.topup.pojo.User;
import com.topup.service.UserService;
import com.topup.service.serviceimpl.UserServiceimpl;
import com.topup.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
     private UserServiceimpl userServiceimpl;
    @RequestMapping("login")
    @ResponseBody
    public Result login(HttpServletRequest request, HttpServletResponse response) throws  Exception{
        User u;
        Result r=new Result();
        String name1=request.getParameter("username");
        String pwd1=request.getParameter("userpwd");
        System.out.println("123123");
        System.out.println(name1);
        System.out.println(pwd1);

       u=userServiceimpl.Userlogin(name1,pwd1);
        System.out.println(u);
        if(u!=null){
            r.setStatus("0");
            request.getSession().setAttribute("user",u );
            //创建cookie1，为其指定键名是userName，值是输入的用户名。
            r.setMsg("登陆");

        }else{
            r.setStatus("1");
            r.setMsg("登陆失败,请检查账号或者密码");
        }






        return r;
    }
    @RequestMapping("add")
    @ResponseBody
    public Result adduser(HttpServletRequest request, HttpServletResponse response) throws  Exception {

        Result r = new Result();
        String name1 = request.getParameter("name");
        String pwd1 = request.getParameter("pwd");
        String num = request.getParameter("num");
        String solo = request.getParameter("solo");
        System.out.println("天机图");
        User u = new User(name1, pwd1, num, Integer.parseInt(solo));
        System.out.println(u.getName());
        int a = userServiceimpl.adduser(u);
        if (a != 0) {
            r.setStatus("0");

            System.out.println("天机图2");
        } else {
            r.setStatus("1");
            r.setMsg("登陆失败,请检查账号或者密码");
        }

        return r;
    }
}
